﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Resn.WhuPub.BLL;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现省/直辖市的属性和方法
        /// </summary>
        public class Province : Entity<ProvinceModel>
        {
                #region 构造函数

                /// <summary>
                ///初始化<see cref="Province" />省/直辖市.
                /// </summary>
                /// <param name="model">省/直辖市模型</param>
                public Province(ProvinceModel model)
                        : base(model)
                {
                }

                #endregion 构造函数

                #region 属性

                #region 基本属性

                /// <summary>
                ///获取省/直辖市的名称
                /// </summary>
                /// <value>
                ///省/直辖市的名称
                /// </value>
                public string Name
                {
                        get { return Model.Name; }
                        set { Model.Name = value; }
                }

                #endregion 基本属性

                /// <summary>
                /// 获取省/直辖市及城市集合
                /// </summary>
                /// <value>
                /// 省/直辖市及城市集合
                /// </value>
                public static Dictionary<string, List<string>> ProvinceCityStrings
                {
                        get
                        {
                                var ids = new Dictionary<string, string>();
                                var result = new Dictionary<string, List<string>>();

                                var provinceModels = DBStorage.Instance.Select<ProvinceModel>();
                                foreach (var provinceModel in provinceModels)
                                {
                                        ids.Add(provinceModel.Id, provinceModel.Name);
                                        result.Add(provinceModel.Name, new List<string>());
                                }

                                var cityModels = DBStorage.Instance.Select<CityModel>();
                                foreach (var cityModel in cityModels)
                                {
                                        result[ids[cityModel.ProvinceId]].Add(cityModel.Name);
                                }

                                return result;
                        }
                }


                /// <summary>
                /// 获取省/直辖市及城市集合
                /// </summary>
                /// <value>
                /// 省/直辖市及城市集合
                /// </value>
                public static Dictionary<Province, ObservableCollection<City>> ProvinceCitys
                {
                        get
                        {
                                var result = new Dictionary<Province, ObservableCollection<City>>();

                                var provinceModels = DBStorage.Instance.Select<ProvinceModel>();
                                foreach (var model in provinceModels)
                                {
                                        var province = new Province(model);
                                        result.Add(province, province.Citys);
                                }

                                return result;
                        }
                }

                /// <summary>
                /// 获取省/直辖市集合
                /// </summary>
                /// <value>
                /// 省/直辖市集合
                /// </value>
                public static ObservableCollection<Province> Provinces
                {
                        get
                        {
                                var result = new ObservableCollection<Province>();

                                var provinceModels = DBStorage.Instance.Select<ProvinceModel>();
                                foreach (var model in provinceModels)
                                {
                                        result.Add(new Province(model));
                                }

                                return result;
                        }
                }

                /// <summary>
                ///获取省/直辖市的城市
                /// </summary>
                /// <value>
                ///省/直辖市的城市
                /// </value>
                public ObservableCollection<City> Citys
                {
                        get
                        {
                                var models =
                                        DBStorage.Instance.Select<CityModel>()
                                                 .Where(i => i.ProvinceId == Id);

                                var result = new ObservableCollection<City>();

                                foreach (var model in models)
                                {
                                        result.Add(new City(model));
                                }

                                return result;
                        }
                }

                

                #endregion 属性

                /// <summary>
                /// 创建省/直辖市
                /// </summary>
                /// <param name="name">名称</param>
                /// <returns>
                /// 创建的省/直辖市对象
                /// </returns>
                public static Province CreateProvince(string name)
                {
                        var model = new ProvinceModel
                        {
                                Id = Guid.NewGuid().ToString(),
                                Name = name,
                        };

                        return new Province(model);
                }

                /// <summary>
                ///创建城市
                /// </summary>
                /// <param name="name">名称</param>
                /// <returns>
                ///非null:创建成功，返回创建的城市对象
                ///null：创建失败
                /// </returns>
                public City CreateCity(string name)
                {
                        var model = new CityModel
                        {
                                Id = Guid.NewGuid().ToString(),
                                ProvinceId = Id,
                                Name = name,
                        };

                        return new City(model);
                }
                
        }
}